package action;

import java.util.List;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.struts.action.Action;
import org.apache.struts.action.ActionForm;
import org.apache.struts.action.ActionForward;
import org.apache.struts.action.ActionMapping;
import org.apache.struts.action.ActionMessage;
import org.apache.struts.action.ActionMessages;

import test.login.User;
import test.login.UserController;
import test.login.UserSearch;

import common.LoginInfo;

public class LoginAction extends Action {
	@Override
	public ActionForward execute(ActionMapping mapping, ActionForm form,
			HttpServletRequest request, HttpServletResponse response)
			throws Exception {

		LoginInfo li = (LoginInfo) form;
		UserController uc = new UserController();
		UserSearch us = new UserSearch();

		us.setId(li.getLoginId());
		us.setUser(li.getLoginPass());

		List<User> userList = uc.checkLogin(us);

		if (userList.size() > 0) {
			// 成功
			return mapping.findForward("read");
		} else {
			// エラー情報生成
			ActionMessages errors = new ActionMessages();
			errors.add("login", new ActionMessage("errors.login",
					"ログインＩＤまたはパスワード"));

			// エラー情報をセッションに保存する。
			saveErrors(request, errors);
		}

		// TODO 自動生成されたメソッド・スタブ
		return mapping.findForward("success");
	}
}
